package com.generator.model;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据库表信息
 * 
 * @author Generator
 * @since 2024-01-01
 */
@Data
public class TableInfo {
    
    /** 表名 */
    private String tableName;
    
    /** 表注释 */
    private String tableComment;
    
    /** 实体类名 */
    private String entityName;
    
    /** 列信息列表 */
    private List<ColumnInfo> columns;
    
    /** 主键列 */
    private ColumnInfo primaryKeyColumn;
    
    /** 包名 */
    private String packageName;
    
    /** 作者 */
    private String author;

    public TableInfo() {
        this.columns = new ArrayList<>();
    }

    public TableInfo(String tableName) {
        this();
        this.tableName = tableName;
        this.entityName = toClassName(tableName);
    }

    /**
     * 将表名转换为类名（大驼峰命名法）
     */
    private String toClassName(String tableName) {
        if (tableName == null || tableName.isEmpty()) {
            return tableName;
        }
        
        // 如果已经是驼峰命名，直接首字母大写
        if (!tableName.contains("_")) {
            return Character.toUpperCase(tableName.charAt(0)) + tableName.substring(1);
        }
        
        // 处理下划线分隔的命名
        StringBuilder result = new StringBuilder();
        boolean nextUpperCase = true;
        
        for (char c : tableName.toCharArray()) {
            if (c == '_') {
                nextUpperCase = true;
            } else if (nextUpperCase) {
                result.append(Character.toUpperCase(c));
                nextUpperCase = false;
            } else {
                result.append(Character.toLowerCase(c));
            }
        }
        
        return result.toString();
    }

    /**
     * 添加列信息
     */
    public void addColumn(ColumnInfo column) {
        if (column != null) {
            this.columns.add(column);
            if (column.isPrimaryKey()) {
                this.primaryKeyColumn = column;
            }
        }
    }

    /**
     * 获取需要导入的Java类型
     */
    public List<String> getImportTypes() {
        List<String> imports = new ArrayList<>();
        
        for (ColumnInfo column : columns) {
            String javaType = column.getJavaType();
            if ("LocalDate".equals(javaType) && !imports.contains("java.time.LocalDate")) {
                imports.add("java.time.LocalDate");
            } else if ("LocalDateTime".equals(javaType) && !imports.contains("java.time.LocalDateTime")) {
                imports.add("java.time.LocalDateTime");
            } else if ("BigDecimal".equals(javaType) && !imports.contains("java.math.BigDecimal")) {
                imports.add("java.math.BigDecimal");
            }
        }
        
        return imports;
    }

    /**
     * 获取非主键列
     */
    public List<ColumnInfo> getNonPrimaryColumns() {
        List<ColumnInfo> nonPrimaryColumns = new ArrayList<>();
        for (ColumnInfo column : columns) {
            if (!column.isPrimaryKey()) {
                nonPrimaryColumns.add(column);
            }
        }
        return nonPrimaryColumns;
    }

    /**
     * 获取实体类名的小写形式
     */
    public String getEntityNameLowerCase() {
        if (entityName == null || entityName.isEmpty()) {
            return entityName;
        }
        return Character.toLowerCase(entityName.charAt(0)) + entityName.substring(1);
    }

    /**
     * 获取主键的Java类型
     */
    public String getPrimaryKeyJavaType() {
        return primaryKeyColumn != null ? primaryKeyColumn.getJavaType() : "Long";
    }

    /**
     * 获取主键的属性名
     */
    public String getPrimaryKeyProperty() {
        return primaryKeyColumn != null ? primaryKeyColumn.getPropertyName() : "id";
    }

    @Override
    public String toString() {
        return "TableInfo{" +
                "tableName='" + tableName + '\'' +
                ", tableComment='" + tableComment + '\'' +
                ", entityName='" + entityName + '\'' +
                ", columns=" + columns.size() +
                ", primaryKeyColumn=" + (primaryKeyColumn != null ? primaryKeyColumn.getColumnName() : "null") +
                '}';
    }
}